package com.util;


import lombok.*;

/**
 * AIKA
 * 封装if,变为流式写法
 */

@Data
@AllArgsConstructor
@NoArgsConstructor
public class IfUtil {

    public Boolean flag;

    @FunctionalInterface
    public interface Action<T> {
        void then(T t);
    }

    public IfUtil then(Action action) {
        if (this.getFlag()) {
            action.then(flag);
        }
        return this;
    }

    public void Else(Action action) {
        if (!this.getFlag()) {
            action.then(flag);
        }
    }

    public static IfUtil If(boolean flag) {
        return new IfUtil(flag);
    }

    public void Throw(String message) {
        if (flag) {
            throw new MyException(message);
        }
    }


    public static void main(String[] args) {
        IfUtil.If(true).then(s -> System.out.println(1)).
                Else(s -> System.out.println(0));
        IfUtil.If(true).Throw("error");
    }

}
